int *f[N], mem[N];

void solve()
{
	int n;
	cin >> n;

	vector<vector<int>> g(n + 1);
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	vector<int> son(n + 1, 0), len(n + 1), ans(n + 1);
	auto dfs1 = [&](auto &&dfs1, int u, int fa) -> void {
		len[u] = 0;
		for (auto &v : g[u]) {
			if (v != fa) {
				dfs1(dfs1, v, u);
				if ((!son[u]) || (len[son[u]] < len[v])) {
					son[u] = v;
				}
			}
		}
		len[u] = len[son[u]] + 1;
	};

	int *now = mem;
	auto dfs2 = [&](auto &&dfs2, int u, int fa) -> void {
		f[u][0] = 1;
		if (son[u]) {
			f[son[u]] = f[u] + 1;
			dfs2(dfs2, son[u], u);
			ans[u] = ans[son[u]] + 1;
		}
		for (auto &v : g[u]) {
			if (v != fa && v != son[u]) {
				f[v] = now;
				now += len[v];
				dfs2(dfs2, v, u);
				for (int j = 1; j <= len[v]; j++) {
					f[u][j] += f[v][j - 1];
					if ((f[u][j] > f[u][ans[u]]) || (f[u][j] == f[u][ans[u]] && j < ans[u])) {
						ans[u] = j;
					}
				}
			}
		}
		if (f[u][ans[u]] == 1) {
			ans[u] = 0;
		}
	};
	dfs1(dfs1, 1, 0);
	f[1] = now; now += len[1];
	dfs2(dfs2, 1, 0);
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << '\n';
	}
}